<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeRoleDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <!--
      1. 视图 -> 数据 当我们通过选中操作 选中项的label值会自动放到checkList中
      2. 数据 -> 视图 checkList中如果有值 checkbox谁的label在里面存在 它就会显示选中状态
     -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="confirm">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      checkList: [],
      roleList: [] // 角色列表
    }
  },
  methods: {
    closeRoleDialog () {
      this.$emit('close-dialog')
    },
    async openDialog () {
      // 获取角色列表
      const res = await getRoleList({ page: 1, pagesize: 50 })
      this.roleList = res.data.rows
      // 获取一个个人信息
      const resDetail = await getUserDetailById(this.curId)
      console.log(resDetail)
      this.checkList = resDetail.data.roleIds
    },

    async confirm () {
      await assignRoles({
        id: this.curId,
        roleIds: this.checkList
      })
      this.closeRoleDialog()
    }
  }
}

</script>
